package giro.general.ranking;

import giro.general.BaseActivity;
import giro.general.R;
import giro.general.util.web.WebViewGiro;

import java.util.GregorianCalendar;
import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class RankingFinderActivity extends BaseActivity {

	private String url = "http://www.gazzetta.it/Speciali/Giroditalia/2012/classifiche/it/t_";
	private String lingua;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ranking_finder);
		initToolbar();

		if (Locale.getDefault().equals(Locale.ITALIAN) || Locale.getDefault().equals(Locale.ITALY)) {
			lingua = "it";
		} else {
			lingua = "en";
		}

		Spinner spinnerTappa = (Spinner) findViewById(R.id.spinner_tappa);
		ArrayAdapter<CharSequence> adapterTappa = ArrayAdapter.createFromResource(this, R.array.stage_array,
				android.R.layout.simple_spinner_item);
		adapterTappa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTappa.setAdapter(adapterTappa);

		spinnerTappa.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 != 0) {
					String temp = "";
					if (arg2 < 10) {
						temp = "0" + arg2;
					} else {
						temp = arg2 + "";
					}
					Intent intent = new Intent(RankingFinderActivity.this, WebViewGiro.class);
					intent.putExtra("url", url + temp + "_class.shtml?lang=" + lingua);
					startActivity(intent);
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});

		Spinner spinnerTipo = (Spinner) findViewById(R.id.spinner_tipo);
		ArrayAdapter<CharSequence> adapterTipo = ArrayAdapter.createFromResource(this, R.array.jersey_array,
				android.R.layout.simple_spinner_item);
		adapterTipo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTipo.setAdapter(adapterTipo);

		spinnerTipo.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 != 0) {
					Intent intent = new Intent(RankingFinderActivity.this, WebViewGiro.class);

					String tappa = searchStageFromDay();

					if (arg2 == 1) {
						intent.putExtra("url", url + tappa + "_class_CLGEN.shtml?lang=" + lingua);
					} else if (arg2 == 2) {
						intent.putExtra("url", url + tappa + "_class_CLPUNGEN.shtml?lang=" + lingua);
					} else if (arg2 == 3) {
						intent.putExtra("url", url + tappa + "_class_CLGPMGEN.shtml?lang=" + lingua);
					} else if (arg2 == 4) {
						intent.putExtra("url", url + tappa + "_class_CLGENGIO.shtml?lang=" + lingua);
					}
					startActivity(intent);
				}
			}

			private String searchStageFromDay() {
				GregorianCalendar gc = new GregorianCalendar();
				int giorno = gc.get(GregorianCalendar.DAY_OF_MONTH);
				int mese = (gc.get(GregorianCalendar.MONTH)) + 1;
				int anno = gc.get(GregorianCalendar.YEAR);

				if (mese == 5 && anno == 2012) {
					switch (giorno) {
					case 11:
						return "05";
					case 12:
						return "06";
					case 13:
						return "07";
					case 14:
						return "08";
					case 15:
						return "09";
					case 16:
						return "10";
					case 17:
						return "11";
					case 18:
						return "12";
					case 19:
						return "13";
					case 20:
						return "14";
					case 21:
						return "15";
					case 22:
						return "15";
					case 23:
						return "16";
					case 24:
						return "17";
					case 25:
						return "18";
					case 26:
						return "19";
					case 27:
						return "20";
					default:
						return "21";
					}
				} else {
					return "21";
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
	}
}
